﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.SFA.Server.Entity;

/// <summary>
/// 常规指标维护
/// Update By:Allen Jiang
/// Update Time:2008-12-26
/// Description:
///     修改常规指标维护在一个页面。
/// </summary>
public partial class InStoreDataMeasures_MeasuresGroup_List : PageBase
{
    private const string MeasuresGroup_List = "MeasuresGroup_List.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetMeasuresGroupBind();
        }
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void GetMeasuresGroupBind()
    {
       GridPager1.DataSource = new MeasuresGroupManager().GetMeasuresGroupTable(MyStateManager.DomainID, ddlValid.SelectedValue);
       GridPager1.GridView = GridView1;
       GridPager1.BindData();
       GridPager1.Visible = GridView1.Rows.Count > 0;
       
    }
  
    /// <summary>
    /// 保存数据事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
       
        try
        {
            MEASURE_GROUP MGROUP = null;
            MeasuresGroupManager manager = new MeasuresGroupManager();

            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                MGROUP = new MEASURE_GROUP();
                MGROUP.CREATETIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);

            }
            else
            {
                MGROUP = manager.Load(int.Parse(GridView1.SelectedDataKey.Value.ToString()));
            }
            TextBox txtCode = DetailsView1.FindControl("txtCode") as TextBox;
            TextBox txtName = DetailsView1.FindControl("txtName") as TextBox;
            TextBox txtRemark = DetailsView1.FindControl("txtRemark") as TextBox;
            MGROUP.CODE = txtCode.Text;
            MGROUP.NAME = txtName.Text;
            MGROUP.REMARK = txtRemark.Text;
            CheckBox cbxValid = DetailsView1.FindControl("chkValid") as CheckBox;
            MGROUP.ACTIVE = cbxValid.Checked;
            MGROUP.DOMAINID = MyStateManager.DomainID;
            MGROUP.LAST_UPDATER = MyStateManager.UserID.ToString();
            MGROUP.LAST_UPDATE_TIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);

            string LogContent = "";

            if (MGROUP.State == DataRowState.Added)
            {
                LogContent += "添加指标组：" + txtName.Text;
            }
            else
            {
                LogContent += MGROUP.CODE+ "信息发生变动，变动后信息是：";
                LogContent += "指标组名称：" + MGROUP.NAME;
                LogContent += "有效：" + MGROUP.ACTIVE;
                LogContent += "操作人：" + MGROUP.LAST_UPDATER;
            }
            if (MGROUP.State == DataRowState.Added)
            {
                if (manager.Load(MGROUP.DOMAINID, MGROUP.CODE) != null)
                {
                    ShowMessage("代码已存在."); return;
                }
            }
            manager.Save(MGROUP);

            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(Jumpton.SFA.Server.BusinessRule.System.LogType.AUTOSYNC,
            MyStateManager.Name, MyStateManager.UserID,LogContent);

            manager = null;
            
            ShowMessage("保存成功",Request.Url.ToString());
            
            
          
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                this.MyStateManager.UserID, "常规指标类新增 - 异常:" + ex.Message);
        }
    }

   
    /// <summary>
    /// 编辑事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        DetailsView1.DataBind();
      
    }
    /// <summary>
    /// 绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        CheckBox chk = DetailsView1.FindControl("chkValid") as CheckBox;

        if (DetailsView1.DataItem == null)
        {
            chk.Checked = true;
        }
        else
        {
            TextBox txtCode = DetailsView1.FindControl("txtCode") as TextBox;
            txtCode.Enabled = false;
        }
    }
    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
       
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GetMeasuresGroupBind();
    }
    protected void ddlValid_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetMeasuresGroupBind();
    }
}
